package com.ibeeking.found.upms.g.rest.controller.v1;

import com.ibeeking.found.common.annotation.LoginUser;
import com.ibeeking.found.common.entity.CurrentUser;
import com.ibeeking.found.upms.g.rest.service.IGmcRolePermissionService;
import com.ibeeking.found.upms.g.rest.service.IGmcRoleService;
import com.ibeeking.found.upms.service.common.bo.RolePermissionBO;
import com.ibeeking.found.upms.service.common.param.GmcRoleParam;
import com.ibeeking.found.upms.service.common.query.GmcRolePageQuery;
import com.ibeeking.found.upms.service.common.vo.GmcRolePageVO;
import com.ibeeking.found.upms.service.common.vo.RoleSelectVO;
import com.ibeeking.found.upms.service.common.vo.RoleVO;
import com.ibeeking.nematos.constants.enums.OperateLogTypeEnum;
import com.ibeeking.nematos.log.annotation.Log;
import com.ibeeking.nematos.utils.base.BaseValidationGroup;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName RoleController控制器
 * @Description 全局角色管理
 * @Author ibeeking
 * @Date 2020-12-02 15:29
 **/
@RestController
@RequestMapping("/g/role/v1")
public class GmcRoleController {

    @Resource
    private IGmcRoleService roleServiceImpl;
    @Resource
    private IGmcRolePermissionService rolePermissionServiceImpl;

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "分页查询角色列表")
    @PostMapping(value = "/pageList")
    public Page<GmcRolePageVO> pageList(@RequestBody GmcRolePageQuery gmcRolePageQuery) {
        return roleServiceImpl.pageList(gmcRolePageQuery);
    }

    @Log(logType = OperateLogTypeEnum.SAVE, describe = "新增全局角色")
    @PostMapping(value = "/add")
    public Boolean add(@LoginUser CurrentUser currentUser, @RequestBody @Validated(BaseValidationGroup.Insert.class) GmcRoleParam gmcRoleParam) {
        gmcRoleParam.setCreateBy(currentUser.getId());
        return roleServiceImpl.add(gmcRoleParam);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "修改全局角色")
    @PostMapping(value = "/modify")
    public Boolean modify(@LoginUser CurrentUser currentUser, @RequestBody @Validated(BaseValidationGroup.Update.class) GmcRoleParam gmcRoleParam) {
        gmcRoleParam.setModifyBy(currentUser.getId());
        return roleServiceImpl.modify(gmcRoleParam);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询全局角色")
    @GetMapping(value = "/view/{id}")
    public RoleVO view(@PathVariable("id") Long id) {
        return roleServiceImpl.view(id);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "启用/禁用全局角色")
    @GetMapping(value = "/changeStatus/{id}/{status}")
    public Boolean changeStatus(@LoginUser CurrentUser currentUser, @PathVariable("id") Long id, @PathVariable("status") Integer status) {
        return roleServiceImpl.changeStatus(id, status, currentUser.getId());
    }

    @Log(logType = OperateLogTypeEnum.DEL, describe = "删除全局角色")
    @GetMapping(value = "/del/{id}")
    public Boolean del(@PathVariable("id") Long id) {
        return roleServiceImpl.del(id);
    }

    @Log(logType = OperateLogTypeEnum.DEL, describe = "检查是否管理员")
    @GetMapping(value = "/isAdministrator/{id}")
    public Boolean isAdministrator(@PathVariable("id") Long id) {
        return roleServiceImpl.checkIsAdministrator(id);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "当前用户角色下拉")
    @GetMapping(value = "/selectRoleList")
    public List<RoleSelectVO> selectRoleList(@LoginUser(isFull = true) CurrentUser currentUser) {
        return roleServiceImpl.selectRoleList(currentUser);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "查询用户id列表")
    @GetMapping(value = "/queryUserIdsByRoleId/{roleId}")
    public List<String> queryUserIdsByRoleId(@PathVariable("roleId") @NotNull(message = "roleId不能为空") Long roleId) {
        return roleServiceImpl.queryUserIdsByRoleId(roleId);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "根据角色查询权限id列表")
    @GetMapping(value = "/queryPermissionIdsByRoleId/{roleId}")
    public List<String> queryPermissionIdsByRoleId(@PathVariable("roleId") @NotNull(message = "roleId不能为空") Long roleId) {
        List<RolePermissionBO> rolePermissionBOS = rolePermissionServiceImpl.queryRolePermissionByRoleId(roleId);
        return rolePermissionBOS.parallelStream().map(rolePermissionBO -> String.valueOf(rolePermissionBO.getPermissionId())).distinct().collect(Collectors.toList());
    }
}